package cn.tedu.baking.controller;

import cn.tedu.baking.pojo.dto.UserChangeDTO;
import cn.tedu.baking.pojo.dto.UserLoginDTO;
import cn.tedu.baking.pojo.dto.UserRegDTO;
import cn.tedu.baking.pojo.dto.UserUpdateDTO;
import cn.tedu.baking.pojo.vo.ContentAdminVO;
import cn.tedu.baking.pojo.vo.UserAdminVO;
import cn.tedu.baking.response.JsonResult;
import cn.tedu.baking.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

@RestController
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired
    IUserService userService;
    @PostMapping("reg")
    public JsonResult reg(@RequestBody @Validated UserRegDTO userRegDTO){
        userService.reg(userRegDTO);
        return JsonResult.ok();
    }
    @PostMapping("login")
    public JsonResult login(@RequestBody @Validated UserLoginDTO userLoginDTO){

        return JsonResult.ok(userService.login(userLoginDTO));
    }

    @GetMapping("logout")
    public JsonResult logout(){
        userService.logout();
        return JsonResult.ok();
    }

    @PostMapping("update")
    public JsonResult update(@RequestBody UserUpdateDTO userUpdateDTO){
        userService.update(userUpdateDTO);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAuthority('ADMIN')")
    @GetMapping("")
    public JsonResult list(){
        List<UserAdminVO> list = userService.list();
        return JsonResult.ok(list);
    }
    @PreAuthorize("hasAuthority('ADMIN')")
    @PostMapping("{id}/delete")
    public JsonResult deleteById(@PathVariable Long id){
        userService.deleteById(id);
        return JsonResult.ok();
    }
    @PreAuthorize("hasAuthority('ADMIN')")
    @PostMapping("change")
    public JsonResult change(@RequestBody UserChangeDTO userChangeDTO){
        userService.change(userChangeDTO);
        return JsonResult.ok();
    }

}
